package net.lab1024.sa.admin.module.business.file.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.file.dao.FiletypeDao;
import net.lab1024.sa.admin.module.business.file.domain.entity.FiletypeEntity;
import net.lab1024.sa.admin.module.business.file.domain.form.add.FiletypeAddForm;
import net.lab1024.sa.admin.module.business.file.domain.form.query.FiletypeQueryForm;
import net.lab1024.sa.admin.module.business.file.domain.form.update.FiletypeUpdateForm;
import net.lab1024.sa.admin.module.business.file.domain.vo.FiletypeVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 文件分类 Service
 *
 * @Author AKITA
 * @Date 2025-07-04 14:25:49
 * @Copyright AKITA
 */

@Service
public class FiletypeService {

    @Resource
    private FiletypeDao filetypeDao;

    /**
     * 分页查询
     */
    public PageResult<FiletypeVO> queryPage(FiletypeQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<FiletypeVO> list = filetypeDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(FiletypeAddForm addForm) {
        FiletypeEntity fileEntity = SmartBeanUtil.copy(addForm, FiletypeEntity.class);
        filetypeDao.insert(fileEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(FiletypeUpdateForm updateForm) {
        FiletypeEntity fileEntity = SmartBeanUtil.copy(updateForm, FiletypeEntity.class);
        filetypeDao.updateById(fileEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        filetypeDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry) {
        if (null == docEntry) {
            return ResponseDTO.ok();
        }

        filetypeDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<List<FiletypeVO>> listAll() {
        List<FiletypeEntity> fileEntities = filetypeDao.selectList(null);
        List<FiletypeVO> fileVOS = SmartBeanUtil.copyList(fileEntities, FiletypeVO.class);
        return ResponseDTO.ok(fileVOS);
    }

    public ResponseDTO<List<FiletypeVO>> listTree() {
        List<FiletypeEntity> fileEntities = filetypeDao.selectList(null);
        List<FiletypeVO> fileVOS = SmartBeanUtil.copyList(fileEntities, FiletypeVO.class);
        List<FiletypeVO> collect = fileVOS.stream().filter(res -> res.getParentId() == 0).collect(Collectors.toList());

        for (FiletypeVO filetypeVO : collect) {
            buildTree(fileVOS, filetypeVO);
        }
        return ResponseDTO.ok(collect);
    }

    private void buildTree(List<FiletypeVO> fileVOS, FiletypeVO filetypeVO) {
        ArrayList<FiletypeVO> filetypeVOS = new ArrayList<>();
        filetypeVO.setChildren(filetypeVOS);
        for (FiletypeVO fileVO : fileVOS) {
            if (!filetypeVO.getDocEntry().equals(fileVO.getDocEntry()) && fileVO.getParentId().equals(filetypeVO.getDocEntry())) {
                buildTree(fileVOS, fileVO);
                filetypeVO.getChildren().add(fileVO);
            }
        }
    }


    public List<Long> selectTreeIds(Long typeId) {
        ArrayList<Long> longs = new ArrayList<>();
        List<FiletypeEntity> filetypeEntities = filetypeDao.selectList(null);
        longs.add(typeId);
        buildTreeId(typeId,longs,filetypeEntities);
        return longs;
    }

    private void buildTreeId(Long typeId, List<Long> ids, List<FiletypeEntity> filetypeEntities) {
        for (FiletypeEntity filetypeEntity : filetypeEntities) {
            if (filetypeEntity.getParentId().equals(typeId)) {
                ids.add(filetypeEntity.getDocEntry());
                buildTreeId(filetypeEntity.getDocEntry(), ids, filetypeEntities);
            }
        }
    }
}
